package com.stms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.stms.bean.User;
import com.stms.utils.DBUtil;

public class UserDao {

	public User selectUserByUserNameAndPassword(String uName, String pWord) {
		Connection conn = DBUtil.getConnection();
		try {
			PreparedStatement statement = conn.prepareStatement("select * from user where username=? and password=?");
			statement.setString(1, uName);
			statement.setString(2, pWord);
			ResultSet set = statement.executeQuery();
			while (set.next()) {
				User user = new User(uName, pWord);
				user.setId(set.getInt("id"));
				user.setUserName(set.getString("username"));
				user.setPassWord(set.getString("password"));
				user.setRank(set.getInt("rank"));
				return user;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}


	public boolean addUser(User user) {
		Connection conn = DBUtil.getConnection();
		String sql = "INSERT INTO user (`username`, `password`, `rank`) VALUES (?, ?,?)";
		try {
			PreparedStatement statement = conn.prepareStatement(sql);
			statement.setString(1, user.getUserName());
			statement.setString(2, user.getPassWord());
			statement.setInt(3, user.getRank());
			int i = statement.executeUpdate();
			if (i>0){
				return true;
			}else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
